package com.demo.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.module.commonui.R;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;

public class IPEditText extends LinearLayout {
    private final EditText firIPEdit;
    private final EditText secIPEdit;
    private final EditText thirIPEdit;
    private final EditText fourIPEdit;

    private String firstIP ="";
    private String secondIP="";
    private String thirdIP="";
    private String fourthIP="";

    private final IPTextHelper mIPTextHelper = new IPTextHelper();

    public void setIpEditTextWatcher(IpEditTextWatcher ipEditTextWatcher) {
        mIpEditTextWatcher = ipEditTextWatcher;
    }

    private IpEditTextWatcher mIpEditTextWatcher;


    public void setFocusValid(boolean focusValid) {
        mFocusValid = focusValid;
    }

    private boolean mFocusValid = false;

    public IPEditText(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        View view = LayoutInflater.from(context).inflate(R.layout.ip_text_layout, this);

        firIPEdit = view.findViewById(R.id.Fist_Text);
        secIPEdit = view.findViewById(R.id.Second_Text);
        thirIPEdit = view.findViewById(R.id.Third_Text);
        fourIPEdit = view.findViewById(R.id.Four_Text);

        setIPEditTextListener();
    }

    private void afterTextChangeed(@NonNull EditText editText, String ip, TextWatcher watcher){
        editText.removeTextChangedListener(watcher);
        editText.setText(ip);
        editText.setSelection(ip.length());
        editText.addTextChangedListener(watcher);
    }

    public void setIPEditTextListener() {
        //设置第一个IP字段的事件监听
        firIPEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean isFocus = mIPTextHelper.isFocusInNextBtn(s);
                firstIP = mIPTextHelper.getTempIp();
                if (isFocus){
                    if (mFocusValid){
                        secIPEdit.setFocusable(true);
                        secIPEdit.requestFocus();
                    }
                }
                notifyChange();
            }

            @Override
            public void afterTextChanged(Editable s) {
                afterTextChangeed(firIPEdit, firstIP, this);
            }
        });

        //设置第二个IP字段的事件监听
        secIPEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean isFocus = mIPTextHelper.isFocusInNextBtn(s);
                secondIP = mIPTextHelper.getTempIp();
                if (isFocus){
                    if (mFocusValid){
                        thirIPEdit.setFocusable(true);
                        thirIPEdit.requestFocus();
                    }
                }
                notifyChange();
            }

            @Override
            public void afterTextChanged(Editable s) {
                afterTextChangeed(secIPEdit, secondIP, this);
            }
        });

        setEditKeyListener(secIPEdit, firIPEdit);

        //设置第三个IP字段的事件监听
        thirIPEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int after) {
                boolean isFocus = mIPTextHelper.isFocusInNextBtn(s);
                thirdIP = mIPTextHelper.getTempIp();
                if (isFocus){
                    if (mFocusValid){
                        fourIPEdit.setFocusable(true);
                        fourIPEdit.requestFocus();
                    }
                }
                notifyChange();
            }

            @Override
            public void afterTextChanged(Editable s) {
                afterTextChangeed(thirIPEdit, thirdIP, this);
            }
        });

        setEditKeyListener(thirIPEdit, secIPEdit);

        //设置第四个IP字段的事件监听
        fourIPEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mIPTextHelper.isFocusInNextBtn(s);
                fourthIP = mIPTextHelper.getTempIp();
                notifyChange();
            }

            @Override
            public void afterTextChanged(Editable s) {
                afterTextChangeed(fourIPEdit, fourthIP, this);
            }
        });

        setEditKeyListener(fourIPEdit, thirIPEdit);
    }

    private void notifyChange() {
        if (mFocusValid && mIpEditTextWatcher != null){
            mIpEditTextWatcher.change();
        }
    }

    private void setEditKeyListener(final EditText editText, final EditText FocusEdit) {
        editText.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                String content = editText.getText().toString();
                TGLog.d("content = " + content);
                if (StringUtils.isEmpty(content)) {
                    if (mFocusValid) {
                        FocusEdit.setFocusable(true);
                        FocusEdit.requestFocus();
                    }
                }
            }
            return false;
        });
    }

    public String getIpText(){
        if(TextUtils.isEmpty(firstIP) || TextUtils.isEmpty(secondIP)
                || TextUtils.isEmpty(thirdIP) || TextUtils.isEmpty(fourthIP)){
            return  null;
        }
        return  firstIP + "." + secondIP + "." + thirdIP + "." + fourthIP;
    }

    /**
     * 本地读取的ip地址显示至界面
     */
    public void setIpText(String ipText){
        if(TextUtils.isEmpty(ipText) || ipText == null){
            return;
        }
        String text = ipText.trim();
        text = text.replace(" ", "");
        String[] temp = text.split("\\.");
        if(temp.length == 4){
            firIPEdit.setText(IPTextHelper.getValidIP(temp[0]));
            secIPEdit.setText(IPTextHelper.getValidIP(temp[1]));
            thirIPEdit.setText(IPTextHelper.getValidIP(temp[2]));
            fourIPEdit.setText(IPTextHelper.getValidIP(temp[3]));
        }
    }

    public interface IpEditTextWatcher{
        void change();
    }
}